﻿function refreshMore() {

    //获得分类信息
    $.ajax({
        url: "/category/selectAllCategoriesWithArticles",
        type: "POST",
        dataType: 'json',
        data: {},
        success: function (data) {
            var body=document.getElementById("category-content");
            var str="";
            for(i in data){
                if(data[i].articles[0].articleid!=0)
                   str+="<li><a href=\"article-category.html?alias="+data[i].categoryalias+"\" class=\"animsition-link\">"+data[i].categoryname+"<small>&nbsp;("+data[i].articles.length+")</small></a></li>"
            }
            body.innerHTML=str;
        },
        error: function (data) {
            document.getElementById("category-content").innerHTML="  加载失败，请重试."
        }
    });

    //获得标签信息
    $.ajax({
        url: "/tag/selectAllTagsWithArticles",
        type: "POST",
        dataType: 'json',
        data: {},
        success: function (data) {
            var body=document.getElementById("tag-content");
            var str="";
            for(i in data)
                if(data[i].articles.length!=0)
                    str+="<li><a href=\"article-tag.html?alias="+data[i].tagalias+"\" class=\"animsition-link\">"+data[i].tagname+"<small>&nbsp;("+data[i].articles.length+")</small></a></li>"
            body.innerHTML=str;
        },
        error: function (data) {
            document.getElementById("tag-content").innerHTML="  加载失败，请重试."
        }
    });

    //获得友链
    $.ajax({
        url : "/link/findAllFriendLinks",
        type : "POST",
        dataType: 'json',
        data: { },
        success:function(data) {
            <!-- 处理后端返回的数据 -->
            var str = "";
            var tbody=window.document.getElementById("friendlink-content");
            for (i in data) {
                str+="<li><a href=\"\/\/"+data[i].url+"\" class=\"animsition-link\" target=\"_blank\">"+data[i].linkname+"</a></li>"
            }
            tbody.innerHTML = str;
        },
        error: function (data) {
            document.getElementById("friendlink-content").innerHTML="  加载失败，请重试."
        }
    });

}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]); return null;
}

function refreshArticle(topage,perpage) {
    $.ajax({
        url : "/article/selectAllArticles",
        type : "POST",
        dataType: 'json',
        data: {
            "topage":topage,
            "perpage":perpage
        },

        success:function(data) {
            <!-- 处理后端返回的数据 -->
            var str = "";
            var tbody=window.document.getElementById("contain");
            for (i in data.articles) {
                var tags=data.articles[i].grouptags.split(","); //字符分割
                var tagalias=data.articles[i].grouptags2.split(","); //字符分割
                str+="<div class=\"row\">" +
                    "            <div class=\"col-md-8 col-md-offset-2 opening-statement\">" +
                    "                <div class=\"col-md-4\">" +
                    "                    <h3><a href=\"article.html?id="+data.articles[i].articleid+"\">"+data.articles[i].title+"</a></h3>" +
                    "                    <span>" +
                    "                        <span class=\"post-meta\">" +
                    "                            <a>"+data.articles[i].date+"</a>|"
                for(t in tags){
                    str+="                       <a href=\"article-tag.html?alias="+tagalias[t]+"\">"+tags[t]+"</a> ";
                }
                str+="                        </span>" +
                    "                    </span>" +
                    "                </div>" +
                    "                <div class=\"col-md-8\">" +
                    "                    <p>" +
                    data.articles[i].digest+
                    "                    </p>" +
                    "                    <p class=\"pull-right readMore\">" +
                    "                        <a href=\"article.html?id="+data.articles[i].articleid+"\">Read More...</a>" +
                    "                    </p>\n" +
                    "                </div>\n" +
                    "                <div class=\"clearfix\"></div>" +
                    "                <hr class=\"nogutter\">" +
                    "            </div>" +
                    "        </div>";

            }

            curPage=data.page;
            totalPages=data.totalPages;

            str+="<nav class=\"pagination\" role=\"pagination\">\n"
            //  << 前一页
            if(curPage-1>=1)
                str+="<a class=\"pull-left\" href=\"javascript:void(0)\" onclick='refreshArticle(curPage-1,"+perpage+")'>&larr; Newer Posts</a>";
            //  >> 后一页
            if(curPage+1<=totalPages)
                str+="<a class=\"pull-right\" href=\"javascript:void(0)\" onclick='refreshArticle(curPage+1,"+perpage+")'>Older Posts &rarr;</a>";
            str+="</nav>";

            tbody.innerHTML = str;

        },
        error:function(date){
            //window.alert("刷新失败")
            $("#contain").html("  加载失败，请重试.");
        }
    });
}